package com.cloudsigns.factorymanager2.produce.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartOrderBean;
import com.cloudsigns.factorymanager2.produce.iview.IProductOrderAssignRemoveView;
import com.cloudsigns.factorymanager2.produce.iview.IProductOrderAssignView;
import com.cloudsigns.factorymanager2.produce.presenter.ProductOrderAssignPresenter;
import com.cloudsigns.factorymanager2.produce.presenter.ProductOrderAssignRemovePresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

public class ProduceDispatchWorkPartOrderActivity extends BaseActivity implements IProductOrderAssignView, IProductOrderAssignRemoveView {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_workpart_name, tv_dispatch_way, tv_dispatch_num, tv_receive_people;
    private Button btn_dispatch_order, btn_delete_dispatch;

    private WorkPartBean workPartBean;
    private WorkPartOrderBean workPartOrderBean;

    private ProductOrderAssignPresenter productOrderAssignPresenter;
    private ProductOrderAssignRemovePresenter productOrderAssignRemovePresenter;

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_produce_dispatch_workpart_order);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        tv_workpart_name = findViewById(R.id.tv_workpart_name);
        tv_dispatch_way = findViewById(R.id.tv_dispatch_way);
        tv_dispatch_num = findViewById(R.id.tv_dispatch_num);
        tv_receive_people = findViewById(R.id.tv_receive_people);
        btn_dispatch_order = findViewById(R.id.btn_dispatch_order);
        btn_delete_dispatch = findViewById(R.id.btn_delete_dispatch);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText(getResources().getString(R.string.order_dispatch));

        Intent intent = getIntent();
        if (null != intent) {
            workPartBean = (WorkPartBean) intent.getSerializableExtra(ConstantUtil.KEY_1);
            workPartOrderBean = (WorkPartOrderBean) intent.getSerializableExtra(ConstantUtil.KEY_2);
        }

        tv_workpart_name.setText(workPartBean.getWorkPartName());
        tv_dispatch_way.setText("内派");
        tv_dispatch_num.setText(workPartOrderBean.getAmount() + "");
        tv_receive_people.setText(workPartOrderBean.getAssignReceiveAccountName() + ":" + workPartOrderBean.getAssignReceiveAccountId());


        productOrderAssignPresenter = new ProductOrderAssignPresenter(this);
        productOrderAssignRemovePresenter = new ProductOrderAssignRemovePresenter(this);

        if (TextUtils.isEmpty(workPartOrderBean.getWorkPartOrderId()) || workPartOrderBean.getAssignReceiveStatus() == null || workPartOrderBean.getAssignReceiveStatus() == CommonEnum.ProduceOrderStatus.CREATED) {
            btn_dispatch_order.setBackgroundResource(R.drawable.btn_green_submit);
        } else {
            btn_dispatch_order.setBackgroundResource(R.mipmap.btn_long_bg_gray);
            btn_dispatch_order.setEnabled(false);
        }
    }

    @Override
    public void setListener() {
        rl_back.setOnClickListener(this);
        btn_dispatch_order.setOnClickListener(this);
        btn_delete_dispatch.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            // 工单分派
            case R.id.btn_dispatch_order:
                productOrderAssignPresenter.productOrderAssign();
                break;
            // 删除派工
            case R.id.btn_delete_dispatch:
                LayoutInflater inflater = getLayoutInflater();
                View view1 = inflater.inflate(R.layout.dialog_delete, null);
                Button btn_cancle = view1.findViewById(R.id.btn_cancel);
                Button btn_sub = view1.findViewById(R.id.btn_submit);
                TextView tv_content = view1.findViewById(R.id.tv_content);
                final Dialog dialog = new Dialog(ProduceDispatchWorkPartOrderActivity.this);
                Context context = dialog.getContext();
                try {
                    int divierId = context.getResources().getIdentifier("android:id/titleDivider", null, null);
                    View divider = dialog.findViewById(divierId);
                    divider.setBackgroundColor(Color.TRANSPARENT);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                dialog.show();
                Window window = dialog.getWindow();
                WindowManager.LayoutParams lp = window.getAttributes();
                window.setBackgroundDrawable(new ColorDrawable(0));
                window.setGravity(Gravity.CENTER);
                window.setAttributes(lp);
                dialog.setContentView(view1);
                tv_content.setText("确定删除给【" + workPartOrderBean.getAssignReceiveAccountName() + "】的派工吗？");

                // 取消
                btn_cancle.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                });

                // 确定
                btn_sub.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                        if (!TextUtils.isEmpty(workPartOrderBean.getWorkPartOrderId()) && workPartOrderBean.getAssignReceiveStatus() != null) {
                            productOrderAssignRemovePresenter.productOrderAssignRemove();
                        } else {
                            workPartBean.removeWorkpartOrderByIdOrOtherMessage(workPartOrderBean);
                            Intent mIntent = new Intent();
                            mIntent.putExtra(ConstantUtil.KEY_1, workPartBean);
                            setResult(ConstantUtil.START_ACTIVITY_KEY_1, mIntent);
                            showToast("删除成功");
                            finish();
                        }

                    }
                });
                break;
        }
    }

    @Override
    public int getAmount() {
        return workPartOrderBean.getAmount();
    }

    @Override
    public String getAssignReceiveAccountId() {
        return workPartOrderBean.getAssignReceiveAccountId();
    }

    @Override
    public String getOrderId() {
        return workPartBean.getOrderId();
    }

    @Override
    public String getProductId() {
        return workPartBean.getProductId();
    }

    @Override
    public String getWorkPartId() {
        return workPartBean.getWorkPartId();
    }

    @Override
    public String getWorkPartOrderId() {
        return workPartOrderBean.getWorkPartOrderId();
    }

    @Override
    public void getProductOrderAssignRemoveSuccessed() {
        workPartBean.removeWorkpartOrderByIdOrOtherMessage(workPartOrderBean);
        Intent mIntent = new Intent();
        mIntent.putExtra(ConstantUtil.KEY_1, workPartBean);
        setResult(ConstantUtil.START_ACTIVITY_KEY_1, mIntent);
        showToast("删除成功");
        finish();
    }

    @Override
    public void getProductOrderAssignRemoveFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public void getProductOrderAssignSuccessed(WorkPartOrderBean wpBean) {
        for (WorkPartOrderBean wk : workPartBean.getWorkPartOrderList()) {
            if (wk.getWorkPartOrderId().equals(workPartOrderBean.getWorkPartOrderId())) {
                wk.setAssignReceiveStatus(CommonEnum.ProduceOrderStatus.EDITED);
                wk.setWorkPartOrderId(wpBean.getWorkPartOrderId());
            }
        }
        Intent mIntent = new Intent();
        mIntent.putExtra(ConstantUtil.KEY_1, workPartBean);
        setResult(ConstantUtil.START_ACTIVITY_KEY_1, mIntent);
        showToast("分派成功");
        finish();
    }

    @Override
    public void getProductOrderAssignFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
